package com.control.houseon;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.model.houseon.EscenarioBean;



public class ComunicaWebService {
	
	/* String NAMESPACE;
	String METHOD_NAME;
	String SOAP_ACTION;
	String URL;
	
	public ComunicaWebService(String METHOD_NAME){
		this.NAMESPACE = "http://tempuri.org/";
		this.METHOD_NAME = METHOD_NAME;
		this.SOAP_ACTION = this.NAMESPACE + METHOD_NAME;
		this.URL = "http://10.0.2.2:64827/WebServiceHM.asmx";
		//this.URL = "http://192.168.1.103:64827/WebServiceHM.asmx";
	}

	public int validaCasa(String ip, String puerto, String pass) {
		// TODO Auto-generated method stub
		int respuesta = 0;
	        //create a SOAP object
        SoapObject request = new SoapObject("http://tempuri.org/", "ValidaCasa");
        //add method arguments
        request.addProperty("ip", ip);
        request.addProperty("puerto", puerto);
        request.addProperty("password", pass);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpTransportSE transport = new HttpTransportSE(URL);

        try {
            //if you want to debug, uncomment next line
        	//transport.debug = true;
        	transport.call(SOAP_ACTION, envelope);
            SoapObject content = (SoapObject) envelope.bodyIn;
            respuesta = Integer.parseInt(content.getProperty(0).toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
		return respuesta;
	}

	public ArrayList<EscenarioBean> listarEscenarios(int idCasa) {
		// TODO Auto-generated method stub
		ArrayList<EscenarioBean> lista = new ArrayList<EscenarioBean>();
		SoapObject request = new SoapObject("http://tempuri.org/", "ListarEscenarios");
        //add method arguments
        request.addProperty("idCasa", idCasa);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpTransportSE transport = new HttpTransportSE(URL);
        try {
        	transport.call(SOAP_ACTION, envelope);
            SoapObject content = (SoapObject) envelope.bodyIn;
            SoapObject listEscenarios = (SoapObject)content.getProperty(0);
            for (int i = 0; i < listEscenarios.getPropertyCount(); i++) {
            	SoapObject list = (SoapObject)listEscenarios.getProperty(i);
            	SoapObject listTipo=(SoapObject)list.getProperty(1);
				//EscenarioBean es = new EscenarioBean(Integer.parseInt(list.getProperty("Id").toString()), list.getProperty("TipoEscenario").toString(), list.getProperty("Descripcion").toString(), list.getProperty("imagen").toString());
				EscenarioBean es = new EscenarioBean(Integer.parseInt(list.getProperty("Id").toString()), listTipo.getProperty("descripcion").toString(), list.getProperty("Descripcion").toString(), "drawable/man");
				
				lista.add(es);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return lista;
	}
	public Boolean BuscaEscenario(int idEscenario) {
		// TODO Auto-generated method stub
		boolean band= false;
		SoapObject request = new SoapObject("http://tempuri.org/", "BuscarEscenario");
        //add method arguments
        request.addProperty("idescenario", idEscenario);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
       
        HttpTransportSE transport = new HttpTransportSE(URL);
        try {
        	
        	transport.call(SOAP_ACTION, envelope);
            SoapObject content = (SoapObject) envelope.bodyIn;
            SoapObject Escenario = (SoapObject)content.getProperty(0);
            SoapObject listtipo = (SoapObject)Escenario.getProperty(1);
            //ControlHouseMovil.getInstancia().CurrentEscenario = new EscenarioBean(Integer.parseInt(Escenario.getProperty("Id").toString()), Escenario.getProperty("TipoEscenario").toString(), Escenario.getProperty("Descripcion").toString(), Escenario.getProperty("imagen").toString());
            ControlHouseON.getInstancia().CurrentEscenario = new EscenarioBean(Integer.parseInt(Escenario.getProperty("Id").toString()), listtipo.getProperty("descripcion").toString(), Escenario.getProperty("Descripcion").toString(), "drawable/man");
				
				band=true;

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			band =false;
		}
		return band;
	}
	
	public Boolean BuscarDispositivo(int idescenario,int det) {
		// TODO Auto-generated method stub
		boolean band= false;
		SoapObject request = new SoapObject("http://tempuri.org/", "BuscarDispositivo");
        //add method arguments
        request.addProperty("idescenario", idescenario);
        request.addProperty("det", det);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
       
        HttpTransportSE transport = new HttpTransportSE(URL);
        try {
        	
        	transport.call(SOAP_ACTION, envelope);
            SoapObject content = (SoapObject) envelope.bodyIn;
            SoapObject Dispositivo = (SoapObject)content.getProperty(0);

            ControlHouseON.getInstancia().CurrentDispositivo = new DispositivoBean(Integer.parseInt(Dispositivo.getProperty("Id").toString()), Dispositivo.getProperty("Descripcion").toString(), Dispositivo.getProperty("Tipo").toString(), Boolean.parseBoolean(Dispositivo.getProperty("Estado").toString()),"drawable/"+Dispositivo.getProperty("Imagen").toString());
				
				band=true;

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			band =false;
		}
		return band;
	}
	
	public ArrayList<DispositivoBean> listarDispositivos(int idEscenario) {
		// TODO Auto-generated method stub
		ArrayList<DispositivoBean> lista = new ArrayList<DispositivoBean>();
		SoapObject request = new SoapObject("http://tempuri.org/", "ListarDispositivos");
        //add method arguments
        request.addProperty("idescenario", idEscenario);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpTransportSE transport = new HttpTransportSE(URL);
        try {
        	transport.call(SOAP_ACTION, envelope);
            SoapObject content = (SoapObject) envelope.bodyIn;
            SoapObject listdispositivo = (SoapObject)content.getProperty(0);
            for (int i = 0; i < listdispositivo.getPropertyCount(); i++) {
            	SoapObject list = (SoapObject)listdispositivo.getProperty(i);
            	DispositivoBean es = new DispositivoBean(Integer.parseInt(list.getProperty("Id").toString()), list.getProperty("Descripcion").toString(), list.getProperty("Tipo").toString(),Boolean.parseBoolean(list.getProperty("Estado").toString()),"drawable/"+list.getProperty("Imagen").toString());
				lista.add(es);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return lista;
	}
	public Boolean ActualizaEstado(int det) {
		// TODO Auto-generated method stub
		boolean band= false;
		SoapObject request = new SoapObject("http://tempuri.org/", "ActualizarDispositivo");
        //add method arguments
        request.addProperty("idDispoDet", det);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
       
        HttpTransportSE transport = new HttpTransportSE(URL);
        try {
        	
        	transport.call(SOAP_ACTION, envelope);
            SoapObject content = (SoapObject) envelope.bodyIn;
            ControlHouseON.getInstancia().CurrentEstado = Boolean.parseBoolean(content.getProperty(0).toString());
           	
				band=true;

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			band =false;
		}
		return band;
	} */
	
}
